/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.cco;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.cco.exception.LocalException;
import co.gov.medellin.sdep.sisdep.cco.exception.LocalInventarioException;
import co.gov.medellin.sdep.sisdep.cco.exception.LocalInventarioRuntimeException;
import co.gov.medellin.sdep.sisdep.cco.service.ILocalInventarioService;
import co.gov.medellin.sdep.sisdep.cco.service.ILocalService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.model.LocalInventario;

/**
 * @author Usuario
 * 
 */
public class LocalInventarioController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/** bean para el manejo del formulario de busqueda LocalInventario */
	public LocalInventario localInventarioSearch = new LocalInventario();

	/** Objeto contenedor del listado de LocalInventario luego de una consulta. */
	private transient DataModel localInventarioListModel;

	/** bean para el manejo del formulario de creacion/edicion. LocalInventario */
	private LocalInventario localInventario = new LocalInventario();

	/** instancia de servicio localInventarioService */
	private ILocalInventarioService localInventarioService;
	
	private List listaLocales;
	
	/** instancia de servicio localService */
	private ILocalService localService;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public LocalInventarioController() {
		this.localInventarioListModel = new ListDataModel();
		this.listaLocales = new ArrayList();
	}
	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchLocalInventarioByKey() {
		String msg = "";
		try {
			List valores = null;
			localInventarioSearch.setIdLocal(new Long(localInventarioSearch.getIdLocalStr()));
			valores = getLocalInventarioService().load(1, 10,
					this.localInventarioSearch);
			this.localInventarioListModel.setWrappedData(valores);
			try {
				listaLocales();
			} catch (LocalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.localInventarioListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.localInventarioListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.LOCAL_INVENTARIO_SEARCH_VIEW;
	}

  /**
	 * Metodo que permite espeficicar la funcionalidad para la gestion [Crear / Actualizar] de un elmento tipo:
	 * LocalInventario.
	 *     
	 * @author programador.java@consult-soft.com  05-06-2012
	 * @return String Vista a la cual debe retornar
	 * 	 
	 */
	public String manage() {
		String returnView = AppConstants.LOCAL_INVENTARIO_SEARCH_VIEW;
		if(this.localInventario.getId() == null) {
       returnView = create();
		} else {
       returnView = update();
		}
		return returnView;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.localInventario.getId() == null) {
				localInventario.setIdLocal(new Long(localInventario.getIdLocalStr()));
				getLocalInventarioService().insert(this.localInventario);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (LocalInventarioException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (LocalInventarioRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- localInventario " + msg, null, null);
		return AppConstants.LOCAL_INVENTARIO_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.localInventario.getId() != null) {
				localInventario.setIdLocal(new Long(localInventario.getIdLocalStr()));
				getLocalInventarioService().update(this.localInventario);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (LocalInventarioException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (LocalInventarioRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.LOCAL_INVENTARIO_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createLocalInventario() {
		localInventario = new LocalInventario();
		localInventario.setId(null);
		localInventario.setUsuarioCrea(getLoggedUserID());
		localInventario.setUsuarioModifica(getLoggedUserID());
		localInventario.setIdLocalStr(localInventarioSearch.getIdLocalStr());
		//localInventario.setEstado("ACTIVO");
		try {
			listaLocales();
		} catch (LocalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return AppConstants.LOCAL_INVENTARIO_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateLocalInventario() {
		Map params = getExternalContext().getRequestParameterMap();
		String localInventarioDataIndex = (String) params
				.get("localInventarioDataIndex");
		this.localInventario = (LocalInventario) ((List) this.localInventarioListModel
				.getWrappedData())
				.get(Integer.parseInt(localInventarioDataIndex));
		try {
			listaLocales();
		} catch (LocalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return AppConstants.LOCAL_INVENTARIO_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelLocalInventario() {
		return AppConstants.LOCAL_INVENTARIO_SEARCH_VIEW;
	}
	
	public void listaLocales() throws LocalException{
		listaLocales = new ArrayList();
		if(getLocalService().listaLocales() != null){
			for(int i = 0; i<getLocalService().listaLocales().size();i++){			
				List listaT = new ArrayList();
				String registro[];
				listaT = (List)getLocalService().listaLocales().get(i);	
				registro = (String[])listaT.get(0);
				listaLocales.add(new SelectItem(registro[0].toString(),registro[1].toString()));
				listaT = null;
				registro=null;
			}
		}
	}
	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public LocalInventario getLocalInventarioSearch() {
		return localInventarioSearch;
	}

	/**
	 * 
	 * @param localInventarioSearch
	 */
	public void setlocalInventarioSearch(LocalInventario localInventarioSearch) {
		this.localInventarioSearch = localInventarioSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getlocalInventarioListModel() {
		return localInventarioListModel;
	}

	/**
	 * 
	 * @param localInventarioListModel
	 */
	public void setlocalInventarioListModel(DataModel localInventarioListModel) {
		this.localInventarioListModel = localInventarioListModel;
	}

	/**
	 * 
	 * @return
	 */
	public LocalInventario getLocalInventario() {
		return localInventario;
	}

	/**
	 * 
	 * @param localInventario
	 */
	public void setlocalInventario(LocalInventario localInventario) {
		this.localInventario = localInventario;
	}

	/**
	 * obtencion del servicio localInventarioService
	 * 
	 * @return
	 */
	public ILocalInventarioService getLocalInventarioService() {
		return localInventarioService;
	}

	/**
	 * establecimiento del servicio localInventarioService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param localInventarioService
	 */
	public void setLocalInventarioService(
			ILocalInventarioService localInventarioService) {
		this.localInventarioService = localInventarioService;
	}

	public List getListaLocales() {
		return listaLocales;
	}

	public void setListaLocales(List listaLocales) {
		this.listaLocales = listaLocales;
	}

	public ILocalService getLocalService() {
		return localService;
	}

	public void setLocalService(ILocalService localService) {
		this.localService = localService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
